/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bahrie.impl;

import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.bahrie.entity.Kasir;
import com.bahrie.interfaces.InterKasir;

/**
 *
 * @author Saiful Bahri
 * blog    : http://bahrie27.wordpress.com/
 * twitter : @bahrie127
 */
@Repository
public class ImplKasir extends HibernateDaoSupport implements InterKasir {

    @Autowired
    public void setSuperSessionFactory(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }

    public void insert(Kasir o) throws DataAccessException {
        getHibernateTemplate().save(o);
    }

    public void update(Kasir o) throws DataAccessException {
        getHibernateTemplate().saveOrUpdate(o);
    }

    public void delete(int o) throws DataAccessException {
        getHibernateTemplate().delete(o);
    }

    public List<Kasir> getAll() throws DataAccessException {
        return getHibernateTemplate().executeFind(new HibernateCallback() {

            public Object doInHibernate(Session sn) throws HibernateException, SQLException {
                Query queryOb = sn.createQuery("from Kasir k");
                return queryOb.list();
            }
        });
    }

    public Kasir getByUsername(final String o) throws DataAccessException {
        return (Kasir) getHibernateTemplate().execute(new HibernateCallback() {

            public Object doInHibernate(Session sn) throws HibernateException, SQLException {
                Query queryOb = sn.createQuery("from Kasir k where k.username =:username").setParameter("username", o);
                return queryOb.uniqueResult();
            }
        });
    }
}
